<?php

include FCPATH . 'application/controllers/frontend.php';

class Entry extends Frontend {

    public function __construct() {
	parent::__construct();

	$this->load->model('entry_model', 'model');
    }

    public function index() {
	$type = intval($this->uri->segment(3));
	$this->set_javascript_variable('TYPE', $type);
	$data = array('type' => $type);
	$data = $this->prepare_data($type,$data);
	
	if(in_array($type,array(TYPE_LANDED_PROPERTY,TYPE_CITY,TYPE_ROAD,TYPE_LIGHT))){
	    $this->load_javascripts('frontend_project');
	}
	
	if(in_array($type,array(TYPE_JOCO_TRENDS,TYPE_INDUSTRY_TRENDS))){
	    $this->load_javascripts('frontend_trends');
	}
	
	if($type == TYPE_PUBLICATION){
	    $this->load_javascripts('frontend_cultrue_publication');
	}
	
	$this->rend('frontend/entry_index_view', $data);
    }

    public function show() {
	$id = intval($this->uri->segment(4));
	$type = intval($this->uri->segment(3));

	$data = array(
	    'type' => $type,
	    'entry' => $id > 0 ? $this->model->simple_find($id) : $this->model->find_by_type($type)
	);
	
	$data = $this->prepare_data($type, $data);

	if (in_array($type, array(TYPE_TEAM, TYPE_HONOR,TYPE_LANDED_PROPERTY,TYPE_CITY,TYPE_ROAD,TYPE_LIGHT,TYPE_PUBLICATION))) {
	    $this->load->model('photo_model');
	    if ($data['entry']->album) {
		$data['photos'] = $this->photo_model->records(0, 0, array(array('album', $data['entry']->album)));
	    }
	    unset($this->photo_model);
	}

	$this->rend('frontend/entry_show_view', $data);
    }
    
    public function message(){
	$this->load_stylesheets('frontend_contact');
	$this->load_javascripts('frontend_message');
	$this->rend('frontend/message_index_view');
    }
    
    public function insert_message(){
	$data = array(
            'is_display' => 1,
	    'company' => trim($this->input->post('company')),
            'name' => trim($this->input->post('name')),
            'telephone' => trim($this->input->post('telephone')),
            'email' => trim($this->input->post('email')),
            'address' => trim($this->input->post('address')),
            'content' => trim(nl2br($this->input->post('content'))),
            'ip' => $this->input->ip_address(),
            'created_at' => date('Y-m-d H:i:s', time())
        );

	if (empty($data['company'])) {
            $this->json_back('XT002', '请填写您的公司名称');
        }
	
        if (empty($data['name'])) {
            $this->json_back('XT002', '请填写您的称呼');
        }

        if (empty($data['telephone'])) {
            $this->json_back('XT002', '请填写您的电话');
        }

        if (empty($data['email'])) {
            $this->json_back('XT002', '请填写您的邮箱');
        }

        if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
            $this->json_back('XT002', '您的邮箱格式有误');
        }

        if (empty($data['content'])) {
            $this->json_back('XT002', '请填写您的留言内容');
        }

        $this->load->model('message_model');

        if (($id = $this->message_model->insert($data)) > 0) {
            $this->json_back('XT001', '留言成功', array('id' => $id));
        } else {
            $this->json_back('XT002', '服务端数据库错误');
        }
    }

    public function records() {
	$page = intval($this->input->get('page'));
	$page_size = intval($this->input->get('pageSize'));
	$page = $page ? $page : 1;
	$offset = $page_size * ($page - 1);
	$type = intval($this->uri->segment(3));
        $title = trim($this->input->get('title'));
        $title_en = trim($this->input->get('title_en'));
        
        $conditions = array(array('type', $type));
        
        if(!empty($title)){
            $conditions[] = array('title like','%'.$title.'%');
        }
        
        if(!empty($title_en)){
            $conditions[] = array('title_en like','%'.$title_en.'%');
        }

	if (!$type) {
	    $this->json_back('XT002', '错误的类型ID');
	}

	$this->json_back('XT001', '', array(
	    'total' => $this->model->simple_total($conditions),
	    'records' => $this->model->simple_records($page_size, $offset, $conditions)
	));
    }

    private function prepare_data($type, $data) {
	switch ($type) {
	    case TYPE_PROFILE:
	    case TYPE_TEAM:
	    case TYPE_HONOR:
                $data['banner'] = 'sub_about.jpg';
		$data['main_menu'] = array(
		    'label' => 'about',
		    'link' => '/entry/show/' . TYPE_PROFILE
		);

		$data['sub_menu'] = array(
		    TYPE_PROFILE => array(
			'label' => 'profile',
			'link' => '/entry/show/' . TYPE_PROFILE
		    ),
		    TYPE_TEAM => array(
			'label' => 'team',
			'link' => '/entry/show/' . TYPE_TEAM
		    ),
		    TYPE_HONOR => array(
			'label' => 'honor',
			'link' => '/entry/show/' . TYPE_HONOR
		    )
		);
		break;
	    case TYPE_LANDED_PROPERTY:
	    case TYPE_CITY:
	    case TYPE_ROAD:
	    case TYPE_LIGHT:
                $data['banner'] = 'sub_projects.jpg';
		$data['main_menu'] = array('label' => 'projects', 'link' => '/entry/index/' . TYPE_LANDED_PROPERTY);
		$data['sub_menu'] = array(
		    TYPE_LANDED_PROPERTY => array('label' => 'real_estate_landscape','link' => '/entry/index/' . TYPE_LANDED_PROPERTY),
		    TYPE_CITY => array('label' => 'city_comprehensive_projects','link' => '/entry/index/' . TYPE_CITY),
		    TYPE_ROAD => array('label' => 'municipal_road','link' => '/entry/index/' . TYPE_ROAD),
		    TYPE_LIGHT => array('label' => 'lighting_projects','link' => '/entry/index/' . TYPE_LIGHT)
		);
		    break;
	    case TYPE_ACHIEVEMENT:
	    case TYPE_PARTER:
                $data['banner'] = 'sub_strength.jpg';
		$data['main_menu'] = array('label' => 'strength', 'link' => '/entry/show/' . TYPE_ACHIEVEMENT);
		$data['sub_menu'] = array(
		    TYPE_ACHIEVEMENT => array('label' => 'achievements','link' => '/entry/show/' . TYPE_ACHIEVEMENT),
		    TYPE_PARTER => array('label' => 'client','link' => '/entry/show/' . TYPE_PARTER)
		);
		break;
	    case TYPE_JOCO_TRENDS:
	    case TYPE_INDUSTRY_TRENDS:
                $data['banner'] = 'sub_news.jpg';
		$data['main_menu'] = array('label' => 'news', 'link' => '/entry/index/' . TYPE_JOCO_TRENDS);
		$data['sub_menu'] = array(
		    TYPE_JOCO_TRENDS => array('label' => 'company_news','link' => '/entry/index/' . TYPE_JOCO_TRENDS),
		    TYPE_INDUSTRY_TRENDS => array('label' => 'industry_news','link' => '/entry/index/' . TYPE_INDUSTRY_TRENDS)
		);
		break;
	    case TYPE_IDEA:
	    case TYPE_PUBLICATION:
	    case TYPE_DEVELOPMENT_HISTORY:
                $data['banner'] = 'sub_culture.jpg';
		$data['main_menu'] = array('label' => 'culture', 'link' => '/entry/show/' . TYPE_IDEA);
		$data['sub_menu'] = array(
		    TYPE_IDEA => array('label' => 'concept','link' => '/entry/show/' . TYPE_IDEA),
		    TYPE_PUBLICATION => array('label' => 'magazine','link' => '/entry/index/' . TYPE_PUBLICATION),
		    TYPE_DEVELOPMENT_HISTORY => array('label' => 'development_history','link' => '/entry/show/' . TYPE_DEVELOPMENT_HISTORY)
		);
		break;
	    case TYPE_IN_HERE:
	    case TYPE_RECRUIT:
                $data['banner'] = 'sub_HR.jpg';
		$data['main_menu'] = array('label' => 'hr', 'link' => '/entry/show/' . TYPE_IN_HERE);
		$data['sub_menu'] = array(
		    TYPE_IN_HERE => array('label' => 'personnel_idea','link' => '/entry/show/' . TYPE_IN_HERE),
		    TYPE_RECRUIT => array('label' => 'jobs','link' => '/jobs')
		);
		break;
	    case TYPE_CONTACT:
                $data['banner'] = 'sub_contact.jpg';
		$data['main_menu'] = array('label' => 'contact', 'link' => '/entry/show/' . TYPE_CONTACT);
		$data['sub_menu'] = array(
		    TYPE_CONTACT => array('label' => 'contact_us','link' => '/entry/show/' . TYPE_CONTACT),
		    array('label' => 'feedback','link' => '/entry/message')
		);
		break;
	}

	return $data;
    }

}